

// filename ************ EncFIFO.C ***************
// Pointer implementation of a FIFO  
// Jonathan W. Valvano 1/29/04

//  This example accompanies the books
//   "Embedded Microcomputer Systems: Real Time Interfacing",
//         Brooks-Cole, copyright (c) 2000,
//   "Introduction to Embedded Microcomputer Systems: 
//    Motorola 6811 and 6812 Simulation", Brooks-Cole, copyright (c) 2002

// Copyright 2004 by Jonathan W. Valvano, valvano@mail.utexas.edu 
//    You may use, edit, run or distribute this file 
//    as long as the above copyright notice remains 

#include "encfifo.h"  
   
uint8_t static volatile EncLifoIndex = 0;     /* Index of where to put next */
int32_t static EncLifo[ENCLIFOSIZE];  /* The statically allocated fifo data */

/*-----------------------EncLifo_Put----------------------------
  Enter one character into the lifo
  Inputs: 8 bit data
  Outputs: none */
void EncLifo_Put (int32_t data) {  
  EncLifo[EncLifoIndex] = data;
  EncLifoIndex++;
  if(EncLifoIndex >= ENCLIFOSIZE) {EncLifoIndex = 0;}
}

/*-----------------------EncLifo_Get----------------------------
  Remove one character from the fifo
  Inputs: pointer to place to save 8 bit data
  Outputs: true if data is valid */
/*
int32_t EncLifo_Get(void){
  if(EncLifoIndex > 0) {
    return EncLifo[EncLifoIndex - 1];
  } else { //for EncLifoIndex==0
    return EncLifo[ENCLIFOSIZE - 1];
  }
}
*/

//Return the difference between the last two samples
int32_t EncLifo_Diff(void){
  int32_t diff;
  //int32_t diff2;
  cli ();
  if(EncLifoIndex > 1) {
    diff = EncLifo[EncLifoIndex-1] - EncLifo[EncLifoIndex-2];
    //diff2 = EncLifo[EncLifoIndex-] - EncLifo[
  } else if(EncLifoIndex==1){
    diff = EncLifo[EncLifoIndex-1] - EncLifo[ENCLIFOSIZE-1];
  } else if(EncLifoIndex==0){
    diff = EncLifo[ENCLIFOSIZE-1] - EncLifo[ENCLIFOSIZE-2];
  }
  sei ();
  return diff;
}
